"""
@File    :   route_depend.py
@Time    :   2024/03/28 23:35:28
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetUserNameFromAccessTokenDependencies,
)
from src.business.admin.system.tenant.tenant.schemas.out import (
    TenantCompleteOutSchema,
    TenantLoginPageSelectOutSchema,
)
from src.business.admin.system.tenant.tenant.schemas.validate import (
    TenantCreateValidateSchema,
    TenantDeleteValidateSchema,
    TenantListSearchValidateSchema,
    TenantUpdateStatusValidateSchema,
    TenantUpdateValidateSchema,
)
from src.business.admin.system.tenant.tenant.service import TenantService
from src.dependencies.no_introduction import depend_get_master_db_session
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)

from .no_introduction import GetTenantIdDependencies


async def route_depend_get_tenant(
    tenant_id: Annotated[int, Depends(GetTenantIdDependencies.from_query)],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> TenantCompleteOutSchema:
    """查询单个租户的路由依赖

    Args:
        tenant_id (Annotated[int, Depends): 租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        TenantCompleteOutSchema: 租户信息
    """
    tenant: TenantCompleteOutSchema = await TenantService.get_tenant_use_id(
        db_session,
        tenant_id,
        raise_disabled_exception=False,
        raise_expired_exception=False,
    )
    return tenant


async def route_depend_list_login_page_select_tenant(
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> list[TenantLoginPageSelectOutSchema]:
    """查询后台租户选择列表的依赖

    Args:
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        list[TenantLoginPageSelectOutSchema]: 查询结果
    """
    # 查询
    result = await TenantService.list_get_login_page_select_tenant(db_session)
    return result


async def route_depend_list_tenant(
    search_schema: TenantListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[TenantCompleteOutSchema], int]:
    """查询租户列表的路由依赖

    Args:
        search_schema (Annotated[TenantListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[TenantCompleteOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[TenantCompleteOutSchema]
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await TenantService.list_search_complete_tenant(
        db_session, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_create_tenant(
    create_schema: TenantCreateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """创建租户的路由依赖

    Args:
        create_schema (TenantCreateValidateSchema): 创建租户所需的参数
        username: token对应的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        int: 创建实例对应的主键
    """
    # 创建操作
    created_id = await TenantService.create_tenant(
        db_session, create_schema, creator_username=username
    )
    return created_id


async def route_depend_update_tenant(
    update_schema: TenantUpdateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改租户的路由依赖

    Args:
        update_schema (TenantUpdateValidateSchema): 修改租户所需的参数
        username: token对应的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await TenantService.update_tenant(
        db_session, update_schema, updater_username=username
    )


async def route_depend_update_tenant_status(
    update_schema: TenantUpdateStatusValidateSchema,
    username: Annotated[
        str,
        Depends(GetUserNameFromAccessTokenDependencies.from_header),
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """单独修改租户状态的路由依赖

    Args:
        update_schema (TenantUpdateStatusValidateSchema): 修改租户所需的参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await TenantService.update_tenant_status(
        db_session, update_schema, updater_username=username
    )


async def route_depend_delete_tenant(
    delete_schema: TenantDeleteValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除租户的路由依赖

    Args:
        delete_schema (TenantDeleteValidateSchema): 删除租户所需的参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await TenantService.delete_tenant(db_session, delete_schema)
